package com.wwinsoft.framework.web.security;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.wwinsoft.framework.dao.HibernateUtils;
import com.wwinsoft.framework.entity.security.Authority;
import com.wwinsoft.framework.entity.security.Group;
import com.wwinsoft.framework.pojo.common.MenuTree;
import com.wwinsoft.framework.service.security.AccountManager;
import com.wwinsoft.framework.utils.MenuUtils;
import com.wwinsoft.framework.web.CrudActionSupport;
import com.wwinsoft.modules.utils.encode.JsonBinder;
import com.wwinsoft.modules.utils.reflection.ConvertUtils;

@Namespace("/security")
@Results({ @Result(name = CrudActionSupport.RELOAD, location = "role.action", type = "redirect") })
public class RoleAction extends CrudActionSupport<Group> {

    private static final long serialVersionUID = -4052047494894591406L;

    private AccountManager accountManager;

    //-- 页面属性 --//
    private Long id;
    private Group entity;
    private List<Group> allRoleList;//角色列表
    private List<Long> checkedAuthIds;//页面中钩选的权限id列表
 // add by sevenSpend 2011/11/30 -->
    private JsonBinder jsonBinder = JsonBinder.buildNormalBinder();
    private String checkedAuthIdsStr;//页面中勾选的权限id拼接出来的字符串  
 // add by sevenSpend 2011/11/30 <--

    //-- ModelDriven 与 Preparable函数 --//
    public Group getModel() {
        return entity;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    protected void prepareModel() throws Exception {
        if (id!=null) {
            entity = accountManager.getGroupWithAuthorityFilter(id);
        } else {
            entity = new Group();
        }
    }

    //-- CRUD Action 函数 --//
    @Override
    public String list() throws Exception {
        allRoleList = accountManager.getAllRole();
        return SUCCESS;
    }

    @Override
    public String input() throws Exception {
        checkedAuthIds = entity.getAuthIds();
        checkedAuthIdsStr = StringUtils.join(checkedAuthIds, ",") ;
        return INPUT;
    }

    @Override
    public String save() throws Exception {
        checkedAuthIds = ConvertUtils.convertSeparatorStringToList(checkedAuthIdsStr, ",", Long.class);
        //根据页面上的checkbox 整合Role的Authorities Set.
// <-- update by sevenSpend 2011/11/30
        //保存用户并放入成功信息.
        entity.popRoleBaseInfo();
        accountManager.saveGroupInfo(entity, checkedAuthIds);
        addActionMessage("保存角色成功");
        return RELOAD;
    }

 // add by sevenSpend 2011/11/30 -->
     public String getAuthorJson(){
         List<Authority> allAuthor = accountManager.getAllAuthority();
         MenuTree authorTree = MenuUtils.buildMenuTreeWithRoot(allAuthor, "所有权限");
         return jsonBinder.toJson(authorTree);
     }
  // add by sevenSpend 2011/11/30 <--
    @Override
    public String delete() throws Exception {
        accountManager.deleteRole(id);
        addActionMessage("删除角色成功");
        return RELOAD;
    }

    //-- 页面属性访问函数 --//

    /**
     * list页面显示所有角色列表.
     */
    public List<Group> getAllRoleList() {
        return allRoleList;
    }

    /**
     * input页面显示所有授权列表.
     */
    public List<Authority> getAllAuthorityList() {
        return accountManager.getAllAuthority();
    }

    /**
     * input页面显示角色拥有的授权.
     */
    public List<Long> getCheckedAuthIds() {
        return checkedAuthIds;
    }

    /**
     * input页面提交角色拥有的授权.
     */
    public void setCheckedAuthIds(List<Long> checkedAuthIds) {
        this.checkedAuthIds = checkedAuthIds;
    }

    @Autowired
    public void setAccountManager(AccountManager accountManager) {
        this.accountManager = accountManager;
    }

    // add by sevenSpend 2011/11/30 -->
       public String getCheckedAuthIdsStr() {
   		return checkedAuthIdsStr;
   	}

   	public void setCheckedAuthIdsStr(String checkedAuthIdsStr) {
   		this.checkedAuthIdsStr = checkedAuthIdsStr;
   	}
}